﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace becom.DAL
{
	/// <summary>
	/// 数据访问类:oa_appointment
	/// </summary>
	public partial class oa_appointment
	{
		public oa_appointment()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("appointment_id", "oa_appointment"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int appointment_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from oa_appointment");
			strSql.Append(" where appointment_id=@appointment_id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@appointment_id", MySqlDbType.Int32)
};
			parameters[0].Value = appointment_id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(becom.Model.oa_appointment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into oa_appointment(");
			strSql.Append("person_id,bas_person_id,appointment_type,register_date,card_id,appointment_name,relationship,start_date,end_date,appointment_place,appointment_reason,state,note,telephone)");
			strSql.Append(" values (");
			strSql.Append("@person_id,@bas_person_id,@appointment_type,@register_date,@card_id,@appointment_name,@relationship,@start_date,@end_date,@appointment_place,@appointment_reason,@state,@note,@telephone)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@person_id", MySqlDbType.Int32,11),
					new MySqlParameter("@bas_person_id", MySqlDbType.Int32,11),
					new MySqlParameter("@appointment_type", MySqlDbType.Int32,11),
					new MySqlParameter("@register_date", MySqlDbType.DateTime),
					new MySqlParameter("@card_id", MySqlDbType.VarChar,32),
					new MySqlParameter("@appointment_name", MySqlDbType.VarChar,32),
					new MySqlParameter("@relationship", MySqlDbType.VarChar,32),
					new MySqlParameter("@start_date", MySqlDbType.DateTime),
					new MySqlParameter("@end_date", MySqlDbType.DateTime),
					new MySqlParameter("@appointment_place", MySqlDbType.VarChar,64),
					new MySqlParameter("@appointment_reason", MySqlDbType.VarChar,256),
					new MySqlParameter("@state", MySqlDbType.Int32,11),
					new MySqlParameter("@note", MySqlDbType.VarChar,256),
					new MySqlParameter("@telephone", MySqlDbType.VarChar,32)};
			parameters[0].Value = model.person_id;
			parameters[1].Value = model.bas_person_id;
			parameters[2].Value = model.appointment_type;
			parameters[3].Value = model.register_date;
			parameters[4].Value = model.card_id;
			parameters[5].Value = model.appointment_name;
			parameters[6].Value = model.relationship;
			parameters[7].Value = model.start_date;
			parameters[8].Value = model.end_date;
			parameters[9].Value = model.appointment_place;
			parameters[10].Value = model.appointment_reason;
			parameters[11].Value = model.state;
			parameters[12].Value = model.note;
			parameters[13].Value = model.telephone;

			DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(becom.Model.oa_appointment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update oa_appointment set ");
			strSql.Append("person_id=@person_id,");
			strSql.Append("bas_person_id=@bas_person_id,");
			strSql.Append("appointment_type=@appointment_type,");
			strSql.Append("register_date=@register_date,");
			strSql.Append("card_id=@card_id,");
			strSql.Append("appointment_name=@appointment_name,");
			strSql.Append("relationship=@relationship,");
			strSql.Append("start_date=@start_date,");
			strSql.Append("end_date=@end_date,");
			strSql.Append("appointment_place=@appointment_place,");
			strSql.Append("appointment_reason=@appointment_reason,");
			strSql.Append("state=@state,");
			strSql.Append("note=@note,");
			strSql.Append("telephone=@telephone");
			strSql.Append(" where appointment_id=@appointment_id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@person_id", MySqlDbType.Int32,11),
					new MySqlParameter("@bas_person_id", MySqlDbType.Int32,11),
					new MySqlParameter("@appointment_type", MySqlDbType.Int32,11),
					new MySqlParameter("@register_date", MySqlDbType.DateTime),
					new MySqlParameter("@card_id", MySqlDbType.VarChar,32),
					new MySqlParameter("@appointment_name", MySqlDbType.VarChar,32),
					new MySqlParameter("@relationship", MySqlDbType.VarChar,32),
					new MySqlParameter("@start_date", MySqlDbType.DateTime),
					new MySqlParameter("@end_date", MySqlDbType.DateTime),
					new MySqlParameter("@appointment_place", MySqlDbType.VarChar,64),
					new MySqlParameter("@appointment_reason", MySqlDbType.VarChar,256),
					new MySqlParameter("@state", MySqlDbType.Int32,11),
					new MySqlParameter("@note", MySqlDbType.VarChar,256),
					new MySqlParameter("@telephone", MySqlDbType.VarChar,32),
					new MySqlParameter("@appointment_id", MySqlDbType.Int32,11)};
			parameters[0].Value = model.person_id;
			parameters[1].Value = model.bas_person_id;
			parameters[2].Value = model.appointment_type;
			parameters[3].Value = model.register_date;
			parameters[4].Value = model.card_id;
			parameters[5].Value = model.appointment_name;
			parameters[6].Value = model.relationship;
			parameters[7].Value = model.start_date;
			parameters[8].Value = model.end_date;
			parameters[9].Value = model.appointment_place;
			parameters[10].Value = model.appointment_reason;
			parameters[11].Value = model.state;
			parameters[12].Value = model.note;
			parameters[13].Value = model.telephone;
			parameters[14].Value = model.appointment_id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int appointment_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from oa_appointment ");
			strSql.Append(" where appointment_id=@appointment_id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@appointment_id", MySqlDbType.Int32)
};
			parameters[0].Value = appointment_id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string appointment_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from oa_appointment ");
			strSql.Append(" where appointment_id in ("+appointment_idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public becom.Model.oa_appointment GetModel(int appointment_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select appointment_id,person_id,bas_person_id,appointment_type,register_date,card_id,appointment_name,relationship,start_date,end_date,appointment_place,appointment_reason,state,note,telephone from oa_appointment ");
			strSql.Append(" where appointment_id=@appointment_id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@appointment_id", MySqlDbType.Int32)
};
			parameters[0].Value = appointment_id;

			becom.Model.oa_appointment model=new becom.Model.oa_appointment();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["appointment_id"]!=null && ds.Tables[0].Rows[0]["appointment_id"].ToString()!="")
				{
					model.appointment_id=int.Parse(ds.Tables[0].Rows[0]["appointment_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["person_id"]!=null && ds.Tables[0].Rows[0]["person_id"].ToString()!="")
				{
					model.person_id=int.Parse(ds.Tables[0].Rows[0]["person_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["bas_person_id"]!=null && ds.Tables[0].Rows[0]["bas_person_id"].ToString()!="")
				{
					model.bas_person_id=int.Parse(ds.Tables[0].Rows[0]["bas_person_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["appointment_type"]!=null && ds.Tables[0].Rows[0]["appointment_type"].ToString()!="")
				{
					model.appointment_type=int.Parse(ds.Tables[0].Rows[0]["appointment_type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["register_date"]!=null && ds.Tables[0].Rows[0]["register_date"].ToString()!="")
				{
					model.register_date=DateTime.Parse(ds.Tables[0].Rows[0]["register_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["card_id"]!=null && ds.Tables[0].Rows[0]["card_id"].ToString()!="")
				{
					model.card_id=ds.Tables[0].Rows[0]["card_id"].ToString();
				}
				if(ds.Tables[0].Rows[0]["appointment_name"]!=null && ds.Tables[0].Rows[0]["appointment_name"].ToString()!="")
				{
					model.appointment_name=ds.Tables[0].Rows[0]["appointment_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["relationship"]!=null && ds.Tables[0].Rows[0]["relationship"].ToString()!="")
				{
					model.relationship=ds.Tables[0].Rows[0]["relationship"].ToString();
				}
				if(ds.Tables[0].Rows[0]["start_date"]!=null && ds.Tables[0].Rows[0]["start_date"].ToString()!="")
				{
					model.start_date=DateTime.Parse(ds.Tables[0].Rows[0]["start_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["end_date"]!=null && ds.Tables[0].Rows[0]["end_date"].ToString()!="")
				{
					model.end_date=DateTime.Parse(ds.Tables[0].Rows[0]["end_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["appointment_place"]!=null && ds.Tables[0].Rows[0]["appointment_place"].ToString()!="")
				{
					model.appointment_place=ds.Tables[0].Rows[0]["appointment_place"].ToString();
				}
				if(ds.Tables[0].Rows[0]["appointment_reason"]!=null && ds.Tables[0].Rows[0]["appointment_reason"].ToString()!="")
				{
					model.appointment_reason=ds.Tables[0].Rows[0]["appointment_reason"].ToString();
				}
				if(ds.Tables[0].Rows[0]["state"]!=null && ds.Tables[0].Rows[0]["state"].ToString()!="")
				{
					model.state=int.Parse(ds.Tables[0].Rows[0]["state"].ToString());
				}
				if(ds.Tables[0].Rows[0]["note"]!=null && ds.Tables[0].Rows[0]["note"].ToString()!="")
				{
					model.note=ds.Tables[0].Rows[0]["note"].ToString();
				}
				if(ds.Tables[0].Rows[0]["telephone"]!=null && ds.Tables[0].Rows[0]["telephone"].ToString()!="")
				{
					model.telephone=ds.Tables[0].Rows[0]["telephone"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select appointment_id,person_id,bas_person_id,appointment_type,register_date,card_id,appointment_name,relationship,start_date,end_date,appointment_place,appointment_reason,state,note,telephone ");
			strSql.Append(" FROM oa_appointment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "oa_appointment";
			parameters[1].Value = "appointment_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

